package org.springframework.boot.tests.hibernate52.mytest.smartinitializingsingleton;

import com.sun.istack.internal.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.tests.hibernate52.mytest.custombean.ManualRegisterBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNullApi;
import org.springframework.stereotype.Component;

@Component
public class MySmartInitializingSingletonTest implements SmartInitializingSingleton, BeanFactoryAware {

	private BeanFactory beanFactory;

	@Override
	public void afterSingletonsInstantiated() {
		ManualRegisterBean manualRegisterBean = (ManualRegisterBean) beanFactory.getBean(ManualRegisterBean.class.getName());
		manualRegisterBean.onAfterSingletonsInstantiated();
	}


	/**
	 * 在对象进行初始化操作的时候，可以获取到当前的bean工厂
	 * org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeAwareMethods(java.lang.String, java.lang.Object)
	 * <p>
	 * <p>
	 * <p>
	 * 有一个 ApplicationContextAwareProcessor后置处理器，可以实现 获取 ApplicationContext
	 *
	 * @param beanFactory
	 * @throws BeansException
	 */
	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}
}
